import { _decorator, Color, find, Graphics, Layers, Mask, Node, Size, Sprite, SpriteFrame, UITransform, Vec3, view } from 'cc';
import BaseManager from '../../../../extensions/app/assets/base/BaseManager';
import { GuideContext } from './GuideContext';
import { app } from 'db://assets/app/app';
import { GuideEvents } from './GuideEvents';
const { ccclass, property } = _decorator;

@ccclass('GuideManager')
export class GuideManager extends BaseManager {

    @property(Node)
    private guideNode: Node;

    @property(Node)
    private maskNode: Node;

    @property(Node)
    private targetNode: Node

    private guideContext: GuideContext;


    // [无序] 加载完成时触发
    protected onLoad() { }

    // [无序] 自身初始化完成, init执行完毕后被调用
    protected onInited() {

        //加载引导配置

        //创建引导
        this.createGuideNodes();

        app.manager.event.on(GuideEvents.Updata_Guides,this.updateGuides,this)
    }

    private createGuideNodes() {
        const canvas = find("Canvas");
        //创建引导手势节点
        this.guideNode = new Node("guideNode")
        const guideSprite = this.guideNode.addComponent(Sprite);
        this.guideNode.layer = Layers.Enum.UI_2D;
        app.manager.loader.load({
            path: "guides/guide_1/spriteFrame", bundle: 'resources', type: SpriteFrame, onComplete: (spriteFrame) => {
                guideSprite.spriteFrame = spriteFrame
            }
        })

        canvas.addChild(this.guideNode)

        //创建遮罩节点
        this.maskNode = new Node("MaskNode");
        this.maskNode.layer = 1 << 25;
        this.maskNode.active = true
        //添加到Canvas节点下（确保在最顶层）
        canvas.addChild(this.maskNode)

        this.maskNode.addComponent(UITransform);
        this.maskNode.addComponent(Graphics);

        //设置遮罩反向（挖空目标区域）
        const mask = this.maskNode.addComponent(Mask);
        mask.type = Mask.Type.GRAPHICS_RECT;
        this.maskNode.layer = Layers.Enum.UI_2D;

        this.maskNode.setSiblingIndex(Number.MAX_SAFE_INTEGER);

        //添加黑色半透明背景
        const bgNode = new Node("Background");
        const bgSprite = bgNode.addComponent(Sprite);
        bgNode.layer = Layers.Enum.UI_2D;
        bgSprite.color = new Color(0, 0, 0, 150);
        app.manager.loader.load({
            path: "guides/guide_3/spriteFrame", bundle: 'resources', type: SpriteFrame, onComplete: (spriteFrame) => {
                bgSprite.spriteFrame = spriteFrame
                const bgTransform = bgNode.getComponent(UITransform);
                bgTransform.setContentSize(750*2, 1280 * 2)
            }
        })

        //将背景作为遮罩的子节点
        this.maskNode.addChild(bgNode);

        this.maskNode.getComponent(Mask).inverted = true

        // 确保在遮罩挖空层下方
        bgNode.setSiblingIndex(0);
    }

    private updateGuides(worldPos:Vec3,size:Size):void{
        app.lib.logger.log("updateGuides 输出========>>>",worldPos,size)
        if (!this.maskNode) return;
        const maskTransform = this.maskNode.getComponent(UITransform);

        //同步尺寸
        maskTransform.setContentSize(size);
        //同步位置
        this.maskNode.setWorldPosition(worldPos);
    }

    // [无序] 所有manager初始化完成
    protected onFinished() { }

    // [无序] 初始化manager，在初始化完成后，调用finish方法
    protected init(finish: Function) {
        super.init(finish);
    }
}